package org.vordel.incubator.examples.management_api;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.vordel.es.EntityType;

public class TypeManager {

	private Session session;

	TypeManager(Session session) {
		super();
		this.session = session;
	}
	
	
	public void dumpType(String typeName) throws Exception {
		
		EntityType et = this.session.getEntityStore().getTypeForName(typeName);
		et.write(System.out);
		
	}
	
	public void addType(String typeDef) throws Exception {
		
		InputStream in = new ByteArrayInputStream(typeDef.getBytes());
		EntityType et = this.session.getEntityStore().addType(in);
		
		//System.out.println(et.getName());
		
	}
	
	public void updateType(String typeDef) throws Exception {
		
		InputStream in = new ByteArrayInputStream(typeDef.getBytes());
		EntityType et = this.session.getEntityStore().updateType(in);
		
		//System.out.println(et.getName());
		
		
	}
	
	public void addType(InputStream in) throws Exception {
		
		this.session.getEntityStore().addType(in);
		
	}
	
	public void updateType(InputStream in) throws Exception {
		
		this.session.getEntityStore().updateType(in);
		
		
	}
	
	public boolean doesTypeExist(String name) {
		
		try {
			EntityType et = this.session.getEntityStore().getTypeForName(name);
			if (et==null) {
				return false;
			} else {
				return true;
			}
		} catch (Throwable t) {
			//t.printStackTrace();
			System.out.println("Exception from Does Type Exist: "+t.getClass().getName());
			return false;
		}
		
	}
	
}
